﻿Imports System.Data.OleDb
Imports Microsoft.Office.Interop

Public Class fEncaListaPrecios
    Dim daEncaListaPrecios As lbGlobalcom.dsProductosTableAdapters.PRO_ENCALISTAPRECIOSTableAdapter
    Dim daDetListaPrecios As lbGlobalcom.dsProductosTableAdapters.PRO_DETALLELISTAPRECIOSTableAdapter
    Dim daDatosTablas As lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
    Dim daProductos As lbGlobalcom.dsProductosTableAdapters.PRO_PRODUCTOTableAdapter

    Private Sub Salvar(ByVal msj As Boolean)
        Try
            Validate()
            PRO_ENCALISTAPRECIOSBindingSource.EndEdit()
            PRO_DETALLELISTAPRECIOSBindingSource.EndEdit()
            daEncaListaPrecios.Update(DsProductos.PRO_ENCALISTAPRECIOS)
            daDetListaPrecios.Update(DsProductos.PRO_DETALLELISTAPRECIOS)
            If msj Then _
                MessageBox.Show("Se salvarón registros con exito", "Salva Registros", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub PRO_ENCALISTAPRECIOSBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PRO_ENCALISTAPRECIOSBindingNavigatorSaveItem.Click
        Try
            Salvar(True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub fEncaListaPrecios_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        daEncaListaPrecios = New lbGlobalcom.dsProductosTableAdapters.PRO_ENCALISTAPRECIOSTableAdapter
        daDetListaPrecios = New lbGlobalcom.dsProductosTableAdapters.PRO_DETALLELISTAPRECIOSTableAdapter
        daDatosTablas = New lbGlobalcom.dsGlobalTableAdapters.GLO_DATOSTABLASTableAdapter
        daProductos = New lbGlobalcom.dsProductosTableAdapters.PRO_PRODUCTOTableAdapter
        lbGlobalcom.cGeneral.ActivaControles(Me.Controls, False)
        DsProductos.PRO_ENCALISTAPRECIOS.Columns("FECHAINI").DefaultValue = Date.Today
        DsProductos.PRO_ENCALISTAPRECIOS.Columns("FECHAFIN").DefaultValue = Date.Today
        Try
            Dim dtEstado As lbGlobalcom.dsGlobal.GLO_DATOSTABLASDataTable = daDatosTablas.GetDataByIdgrupo("es")
            Dim dtListaDe As lbGlobalcom.dsGlobal.GLO_DATOSTABLASDataTable = daDatosTablas.GetDataByIdgrupo("ld")
            Dim dtTipolista As lbGlobalcom.dsGlobal.GLO_DATOSTABLASDataTable = daDatosTablas.GetDataByIdgrupo("tl")
            EstadoBindingSource.DataSource = dtEstado
            ListaDeBindingSource.DataSource = dtListaDe
            TipoListaBindingSource.DataSource = dtTipolista
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.CellMouseDoubleClick
        Try
            Select Case e.ColumnIndex
                Case 1
                    If PRO_DETALLELISTAPRECIOSDataGridView.CurrentCellAddress.X = 1 Then
                        Dim fLProd As New fListaProductos
                        If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                            PRO_DETALLELISTAPRECIOSDataGridView.Rows(PRO_DETALLELISTAPRECIOSDataGridView.CurrentRow.Index).Cells(1).Value = fLProd.idArticulo
                            Me.txtDescripProducto.Text = daProductos.GetNombreProducto(fLProd.idArticulo)
                        End If
                    End If
            End Select
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.CellValidated
        Try
            If PRO_DETALLELISTAPRECIOSDataGridView.IsCurrentCellInEditMode Then
                Select Case e.ColumnIndex
                    Case 1
                        If PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value IsNot DBNull.Value Then _
                            Me.txtDescripProducto.Text = daProductos.GetNombreProducto(PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
                        'Case 2, 3
                        '    If PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(2).Value IsNot DBNull.Value And PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(3).Value IsNot DBNull.Value Then
                        '        PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(4).Value = (1 - PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(2).Value) * PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(3).Value
                        '    End If

                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.CellValidating
        Try
            If PRO_DETALLELISTAPRECIOSDataGridView.IsCurrentCellInEditMode AndAlso e.FormattedValue IsNot Nothing AndAlso e.FormattedValue <> "" Then
                Select Case e.ColumnIndex
                    Case 1
                        If e.FormattedValue IsNot Nothing AndAlso e.FormattedValue IsNot DBNull.Value AndAlso e.FormattedValue <> "" Then
                            Dim NomProd As Object = daProductos.GetNombreProducto(e.FormattedValue)
                            If NomProd Is Nothing Then
                                MessageBox.Show("Producto con este codigo no existe, verifique e intente de nuevo", "No existe", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                                e.Cancel = True
                            End If
                        End If
                    Case 2
                        If Not IsNumeric(e.FormattedValue) Then
                            e.Cancel = True
                        End If
                    Case 3
                        If Not IsNumeric(e.FormattedValue) Then
                            e.Cancel = True
                        End If
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_DataError(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.DataError
        MessageBox.Show(e.Exception.Message)
    End Sub


    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.GotFocus
        Try
            PRO_ENCALISTAPRECIOSBindingSource.EndEdit()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Try
            lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnBuscaLst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaLst.Click
        Try
            Dim fLPrecios As New fListaPrecios
            If fLPrecios.ShowDialog = Windows.Forms.DialogResult.OK Then
                DsProductos.PRO_DETALLELISTAPRECIOS.Rows.Clear()
                DsProductos.PRO_ENCALISTAPRECIOS.Rows.Clear()
                daEncaListaPrecios.FillByIdLista(DsProductos.PRO_ENCALISTAPRECIOS, fLPrecios.Lista)
                daDetListaPrecios.FillByIdLista(DsProductos.PRO_DETALLELISTAPRECIOS, fLPrecios.Lista)
                lbGlobalcom.cGeneral.ActivaControles(Me.Controls, True)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        
    End Sub

    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.KeyDown
        Try
            If e.Control Then
                Select Case e.KeyCode
                    Case Keys.B
                        If PRO_DETALLELISTAPRECIOSDataGridView.CurrentCellAddress.X = 1 Then
                            Dim fLProd As New fListaProductos
                            If fLProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                                PRO_DETALLELISTAPRECIOSDataGridView.Rows(PRO_DETALLELISTAPRECIOSDataGridView.CurrentRow.Index).Cells(1).Value = fLProd.idArticulo
                                Me.txtDescripProducto.Text = daProductos.GetNombreProducto(fLProd.idArticulo)
                            End If
                        End If
                End Select
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        
    End Sub

    Private Sub PRO_DETALLELISTAPRECIOSDataGridView_RowEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles PRO_DETALLELISTAPRECIOSDataGridView.RowEnter
        Try
            If e.RowIndex >= 0 AndAlso PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(1).Value IsNot Nothing AndAlso PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(1).Value IsNot DBNull.Value Then
                Me.txtDescripProducto.Text = daProductos.GetNombreProducto(PRO_DETALLELISTAPRECIOSDataGridView.Rows(e.RowIndex).Cells(1).Value)
            Else
                Me.txtDescripProducto.Text = ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        
    End Sub


    Private Sub btnBuscaArchivo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaArchivo.Click
        Try
            If ofdRutaArchivo.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtRutaArchivo.Text = ofdRutaArchivo.FileName
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnCargarDatos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCargarDatos.Click
        Try
            If txtRutaArchivo.Text.Length > 0 Then
                Dim cDtalleLista As lbGlobalcom.cDetalleListaPrecios
                'MOV_DETALLENOTASDEBITOBindingSource.EndEdit()
                If txtRutaArchivo.Text.Length > 0 Then
                    Dim cnExcel As OleDb.OleDbConnection
                    Dim rsExcel As OleDb.OleDbDataAdapter
                    Dim ds As New DataSet
                    'Crear una conexión a una hoja de excel
                    cnExcel = New OleDb.OleDbConnection
                    cnExcel.ConnectionString = "Provider = " & My.Settings.ProveedorExcel & ";Data Source=" & txtRutaArchivo.Text & ";Extended Properties=" & My.Settings.PropExtendExcel & ";" 'Aqui esta la clave
                    rsExcel = New OleDb.OleDbDataAdapter("Select * from  [Hoja1$]", cnExcel)
                    'Hacemos una consulta SQL de la hoja excel que quieras traspasar a la base de datos(en este caso, se llama Hoja1)
                    cnExcel.Open()
                    'Rellenar el recordset de Access
                    rsExcel.Fill(ds, "tabla1")
                    cDtalleLista = New lbGlobalcom.cDetalleListaPrecios
                    Dim nRegPro As Integer = 0
                    For Each dr As DataRow In ds.Tables(0).Rows
                        If dr.Item(0) IsNot DBNull.Value Then _
                            cDtalleLista.ActualizaListaPrecios(IDLISTATextBox.Text, dr.Item(0), dr.Item(2), dr.Item(3))
                        nRegPro += 1
                    Next
                    daDetListaPrecios.FillByIdLista(DsProductos.PRO_DETALLELISTAPRECIOS, IDLISTATextBox.Text)
                    MessageBox.Show("Se importarón " & nRegPro.ToString & ", Articulos con exito", "Importa Lista Precios", MessageBoxButtons.OK, MessageBoxIcon.Information)

                Else
                    MessageBox.Show("Debe seleccionar un Archivo", "Archivo", MessageBoxButtons.OK)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnBuscaProducto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscaProducto.Click
        Try
            Dim fProd As New fListaProductos
            If fProd.ShowDialog = Windows.Forms.DialogResult.OK Then
                txtProducto.Text = fProd.idArticulo
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModificar.Click
        Try
            If IDLISTATextBox.Text.Length > 0 Then
                Dim cDtalleLista As New lbGlobalcom.cDetalleListaPrecios
                cDtalleLista.ActualizaListaPrecios(IDLISTATextBox.Text, txtProducto.Text, txtPrecioVenta.Text, txtPrecioCompra.Text)
                daDetListaPrecios.FillByIdLista(DsProductos.PRO_DETALLELISTAPRECIOS, IDLISTATextBox.Text)
                txtProducto.Text = ""
                txtPrecioVenta.Text = ""
                txtPrecioCompra.Text = ""
                btnBuscaProducto.Focus()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnGenerarArchivo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerarArchivo.Click
        Try
            Cursor = Cursors.WaitCursor
            Dim cpro As New lbGlobalcom.cProducto
            Dim ds As New DataSet
            ds = cpro.maestroProductos
            Dim cex As New lbGlobalcom.cExcel(ds)
            Dim m_Excel As Excel.Application
            m_Excel = cex.exportarExcel
            m_Excel.Visible = True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorDeleteItem.Click
        Try
            If MessageBox.Show("Esta seguro que desea eliminar esta lista de precios con todos sus productos", "Elimina Lista Precios", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                PRO_ENCALISTAPRECIOSBindingSource.RemoveCurrent()
                daDetListaPrecios.Update(DsProductos.PRO_DETALLELISTAPRECIOS)
                daEncaListaPrecios.Update(DsProductos.PRO_ENCALISTAPRECIOS)
                MessageBox.Show("Se elimino lista de precios con exito", "Lista Eliminada", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class